package com.example.module_main.ui.ad

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.lib_common.Router
import com.example.lib_common.base.BaseActivity
import com.example.lib_image.GlideApp
import com.example.module_main.AppMMKV
import com.example.module_main.R
import com.example.module_main.databinding.ActivityAdBinding
import com.example.module_main.model.AdModel
import com.example.module_main.model.AdModelItem
import com.example.module_main.ui.splash.SplashViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = Router.Ui.AdActivity)
class AdActivity : BaseActivity<SplashViewModel,ActivityAdBinding>(){
    override val mLayoutResId: Int
        get() = R.layout.activity_ad
    private var adCountDownFinished = false
    private var isOnForward = false //只有页面onResume方可继续

    @Autowired
    lateinit var mAdModel: AdModelItem



    private var mCountDown = 5
    private val mHandler by lazy {
        object : Handler(Looper.getMainLooper()) {
            override fun handleMessage(msg: Message) {
                mCountDown--
                mBinding.tvCountDown.text = String.format("%ds 跳过", mCountDown)
                if (mCountDown == 0) {
                    if (isOnForward) {
                        toNextPage()
                    } else {
                        adCountDownFinished = true
                    }
                } else
                    sendEmptyMessageDelayed(1, 998)
            }
        }
    }

    override fun onResume() {
        super.onResume()
        isOnForward = true
        if (adCountDownFinished) {
            toNextPage()
        }
    }

    private fun toNextPage() {
        if (AppMMKV.needGuide){
            toGuide()
        }else{
            toMain()
        }
    }

    fun toGuide(){
        ARouter.getInstance().build(Router.Ui.GuideActivity).navigation()
    }

    fun toMain(){
        ARouter.getInstance().build(Router.Ui.MainActivity).navigation()
    }

    override fun onPause() {
        super.onPause()
        isOnForward = false
    }

    override fun initData() {
        mHandler.sendEmptyMessageDelayed(1, 998) //开启倒计时
        GlideApp.with(this).load(mAdModel.imageUrl).into(mBinding.iv) //广告图片
    }

    override fun initListener() {
        mBinding.tvCountDown.setOnClickListener {
            mHandler.removeMessages(1)
            toNextPage()
        }
        mBinding.iv.setOnClickListener {
            if (mAdModel?.jumpNativeGoods() == true){
                showToast("跳转到商品详情页面-内部广告")
            }else if (mAdModel?.canJumpWeb() == true){
                showToast("跳转到网页-外部广告")
            }

        }
    }

    override fun onDestroy() {
        super.onDestroy()
        mHandler.removeCallbacksAndMessages(null)
    }
}